package org.maruero.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Properties;

import javax.ejb.EJB;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.maruero.ejb.entity.Book;
import org.maruero.ejb.inter.Books;

public class BookStoreClientInjection extends HttpServlet {
	
	private static final long serialVersionUID = -2515063656288866005L;
	
	@EJB
	private Books books;
	
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/plain");
		ServletOutputStream out = response.getOutputStream();
		out.println("Welcome to the servlet injection ejb client");
		try{
		
//			Properties p = new Properties();
//    		p.put("java.naming.factory.initial", "org.apache.openejb.client.RemoteInitialContextFactory");
//    		p.put("java.naming.provider.url", "http://127.0.0.1:8080/openejb/ejb");
//    		InitialContext context = new InitialContext( p ); 
//    		
//	        Books books = (Books)context.lookup("BooksRemote");
	        books.load();
	        
	        List<Book> bookList = books.getBooks();
	        
	        for( Book book : bookList ){
	        	out.println("--------------------------");
	        	out.println("Title: " + book.getTitle() );
	        	out.println("Author: " + book.getAuthor() );
	        	out.println("Year: " + book.getYear() );
	        }
	        
        
		}catch( Exception e ){
			out.println("Exception caught: " + e.getMessage() );
		}
	}

}
